package com.imooc.service.impl;

import com.imooc.dto.OrderDTO;
import com.imooc.enums.ResultEnum;
import com.imooc.exception.SellException;
import com.imooc.service.PayService;
import com.imooc.utils.JsonUtil;
import com.imooc.utils.MathUtil;
import com.lly835.bestpay.config.WxPayH5Config;
import com.lly835.bestpay.enums.BestPayTypeEnum;
import com.lly835.bestpay.model.PayRequest;
import com.lly835.bestpay.model.PayResponse;
import com.lly835.bestpay.model.RefundRequest;
import com.lly835.bestpay.model.RefundResponse;
import com.lly835.bestpay.service.impl.BestPayServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
@Slf4j
public class PayServiceImpl implements PayService {

    private static final String ORDER_NAME = "微信点餐订单";

    @Autowired
    private BestPayServiceImpl bestPayService;

    @Autowired
    private OrderServiceImpl orderService;


    /**
     * 统一下单
     * @param orderDTO
     * @return
     */
    @Override
    public PayResponse create(OrderDTO orderDTO) {
        PayRequest request=new PayRequest();
        request.setOrderId(orderDTO.getOrderId());//订单id
        request.setOpenid(orderDTO.getBuyerOpenid());//买家的微信id
        request.setOrderAmount(orderDTO.getOrderAmount().doubleValue());//订单的金额
        request.setOrderName(ORDER_NAME);//商品描述
        request.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);//交易类型
        log.info("[微信支付]发起支付 request={}",JsonUtil.toJson(request));
        PayResponse pay = bestPayService.pay(request);
        log.info("[微信支付]发起支付 pay={}", JsonUtil.toJson(pay));
        return pay;
    }

    @Override
    public PayResponse notify(String notifyData) {
        //1. 验证签名
        //2. 支付的状态
        //3. 支付金额
        //4. 支付人(下单人 == 支付人)

        //通过微信支付完成异步返回的结果
        PayResponse payResponse = bestPayService.asyncNotify(notifyData);
        log.info("[微信支付]异步通知 payResponse={}",payResponse);

        //查询订单是否存在
        OrderDTO orderDTO = orderService.findOne(payResponse.getOrderId());
        if(orderDTO==null){
            log.error("【微信支付】异步通知, 订单不存在, orderId={}", payResponse.getOrderId());
            throw new SellException(ResultEnum.ORDER_NOT_EXIST);
        }
        //判断金额是否一致(0.10 0.1)
        if(!MathUtil.bijiao(payResponse.getOrderAmount(),orderDTO.getOrderAmount().doubleValue())){
            log.info("【微信支付】异步通知，金额不一致, payResponse.getOrderAmount()={},orderDTO.getOrderAmount()={}",payResponse.getOrderAmount(),
                    orderDTO.getOrderAmount().doubleValue());
            throw new SellException(ResultEnum.WXPAY_NOTIFY_MONEY_VERIFY_ERROR);
        }

        //修改数据库订单状态
        orderService.paid(orderDTO);
        return null;
    }

    @Override
    public RefundResponse refund(OrderDTO orderDTO) {
        RefundRequest request=new RefundRequest();
        request.setOrderId(orderDTO.getOrderId());
        request.setOrderAmount(orderDTO.getOrderAmount().doubleValue());
        request.setPayTypeEnum(BestPayTypeEnum.WXPAY_H5);
        RefundResponse response = bestPayService.refund(request);
        return response;
    }
}
